/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.data.provider.postgis;

import java.sql.ResultSet;

import chunmap.data.ChunMapDataException;
import chunmap.data.feature.FeatureSchama;
import chunmap.data.feature.LayerMetadata;
import chunmap.data.provider.FeatureDataReader;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.GeometryType;
import chunmap.model.geom.WktReader;

/**
 * @author yangjiandong
 * 
 */
public class PostgisDataSource implements FeatureDataReader {

	private String tableName;
	private String geomColumn;
	private String IdColumn;
	private PostgisConnection pgSql;
	private WktReader wkt = new WktReader();

	
	public PostgisDataSource(String tableName, String geomColumn,
			String idColumn, PostgisConnection pgSql) {
		super();
		this.tableName = tableName;
		this.geomColumn = geomColumn;
		IdColumn = idColumn;
		this.pgSql = pgSql;
	}
	
	//--------------------------------------------------------------------

	@Override
	public Envelope getEnvelop() {
		String sql = "SELECT EXTENT(" + geomColumn + ") FROM " + tableName;
		String str = pgSql.executeScalar(sql).toString();
		Envelope ep = wkt.readEnvelop(str);
		return ep;
	}

	public Geometry getGeometry(long id) {
		String sql = "SELECT Astext(" + geomColumn + ") FROM " + tableName
				+ " WHERE " + IdColumn + "=" + id;
		String str = pgSql.executeScalar(sql).toString();
		return wkt.read(str);
	}
	
	private GeometryType GetShapeType() 
    {
        String sql = "SELECT GeometryType(" + geomColumn + ") FROM " + tableName;
        String str = pgSql.executeScalar(sql).toString();

		if (str.equals("POINT"))
			return GeometryType.Point;
		else if (str.equals("LINESTRING"))
			return GeometryType.LineString;
		else if (str.equals("POLYGON"))
			return GeometryType.Polygon;
		else if (str.equals("MULTIPOINT"))
			return GeometryType.MultiPoint;
		else if (str.equals("MULTILINESTRING"))
			return GeometryType.MultiLineString;
		else if (str.equals("MULTIPOLYGON"))
			return GeometryType.MultiPolygon;
		else
			throw new ChunMapDataException("不能识别的类型");

    }

	@Override
	public Envelope getShapeEnvelop(long id) {
		String sql = "SELECT EXTENT(" + geomColumn + ") FROM " + tableName
				+ " WHERE " + IdColumn + "=" + id;
		String str = pgSql.executeScalar(sql).toString();
		Envelope ep = wkt.readEnvelop(str);
		return ep;
	}

	@Override
	public long size() {
		String sql = "Select count(*) from " + tableName;
		return Long.valueOf(pgSql.executeScalar(sql).toString());
	}

	@Override
	public void close() {
		pgSql.close();
	}

	@Override
	public Object[] data(long index) {
		//String sql = "Select * from " + tableName + " " + where;
        //return pgSql.executeScalar(sql);
		return null;
	}

	@Override
	public FeatureSchama getFeatureSchama() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LayerMetadata getMetadata() {
		LayerMetadata meta = new LayerMetadata(tableName,GetShapeType());
        return meta;
	}

	@Override
	public ResultSet query(String where) {
		String sql = "Select * from " + tableName + " " + where;
        return pgSql.executeScalar(sql);
	}

}
